import { Engine } from "../engine";  
import { Enemy } from "./enemy";  
import { WalkerEnemy } from "./walker-enemy";  
import { Vector2 } from "../vector";  
  
export class EnemyManager {  
  private enemies: Enemy[] = [];  
  
  constructor(private engine: Engine) {}  
  
  spawnEnemy(type: string, pos: Vector2): Enemy {  
    let enemy: Enemy;  
      
    switch (type) {  
      case "walker":  
        enemy = new WalkerEnemy(this.engine, pos);  
        break;  
      default:  
        enemy = new WalkerEnemy(this.engine, pos);  
    }  
      
    this.enemies.push(enemy);  
    return enemy;  
  }  
  
  update_(): void {  
    // 更新所有敌人  
    for (const enemy of this.enemies) {  
      enemy.update_();  
    }  
      
    // 移除死亡的敌人  
    this.enemies = this.enemies.filter(enemy => !enemy.isDead);  
  }  
  
  render(ctx: CanvasRenderingContext2D): void {  
    for (const enemy of this.enemies) {  
      enemy.render(ctx);  
    }  
  }  
  
  getEnemies(): Enemy[] {  
    return this.enemies;  
  }  
  
  clearAllEnemies(): void {  
    this.enemies = [];  
  }  
  
  getEnemyAt(pos: Vector2, radius: number = 20): Enemy | null {  
    for (const enemy of this.enemies) {  
      if (enemy.body_.pos.distanceTo(pos) <= radius) {  
        return enemy;  
      }  
    }  
    return null;  
  }  
}